import java.io.*;

public class Player extends Character {
  int myMoney = 1000;
  int bagSize = 25;
  int myXP = 0;

  public Player(String file) {
    PlayerLoaderSaver cls = new PlayerLoaderSaver();
    equipped.add(headslot);
    equipped.add(handslot);
    equipped.add(bodyslot);
    equipped.add(legslot);
    equipped.add(footslot);
    equipped.add(offslot);
    equipped.add(weaponslot);  
    try {
      ArrayList<Object> info = cls.loadChar(file);
      myVit = (Integer)info.get(0);
      myStn = (Integer)info.get(1);
      myIns = (Integer)info.get(2);
      mySpd = (Integer)info.get(3);
      myFoc = (Integer)info.get(4);
      myHP = (Integer)info.get(5);
      myLvl = (Integer)info.get(6);
      equip(new Helm((String)info.get(7), (Integer)info.get(8), (Integer)info.get(9), (Integer)info.get(10), (Integer)info.get(11)));
      equip(new Gloves((String)info.get(12), (Integer)info.get(13), (Integer)info.get(14), (Integer)info.get(15), (Integer)info.get(16)));
      equip(new Body((String)info.get(17), (Integer)info.get(18), (Integer)info.get(19), (Integer)info.get(20), (Integer)info.get(21)));
      equip(new Pants((String)info.get(22), (Integer)info.get(23), (Integer)info.get(24), (Integer)info.get(25), (Integer)info.get(26)));
      equip(new Shoes((String)info.get(27), (Integer)info.get(28), (Integer)info.get(29), (Integer)info.get(30), (Integer)info.get(31)));
      equip(new Accessory((String)info.get(32), (Integer)info.get(33), (Integer)info.get(34), (Integer)info.get(35), (Integer)info.get(36)));
      equip(new Weapon((String)info.get(37), (Integer)info.get(38), (Integer)info.get(39), (Integer)info.get(40), (Integer)info.get(41), (Integer)info.get(42), (Integer)info.get(43))); 
      myMoney = (Integer)info.get(44);
      int numAbilities = (Integer)info.get(45);
      int i = 0;
      while (i < numAbilities) {
        abilities.add((String)info.get(46+i));
        i++;
      }
      int numInv = (Integer)info.get(46+i);
      int j = i;
      int tracker = 0;
      while (tracker < numInv) {
        if ((Integer)info.get(46+j+1) == 1) {
          inventory.add(new InvSlot(new Weapon((String)info.get(46+j+2), (Integer)info.get(46+j+3), (Integer)info.get(46+j+4), (Integer)info.get(46+j+5), (Integer)info.get(46+j+6), (Integer)info.get(46+j+7), (Integer)info.get(46+j+8))));
          j = j + 8;
        }
        else {
          if ((Integer)info.get(46+j+1) == 2) {
            inventory.add(new InvSlot(new Helm((String)info.get(46+j+2), (Integer)info.get(46+j+3), (Integer)info.get(46+j+4), (Integer)info.get(46+j+5), (Integer)info.get(46+j+6))));
            j = j + 6;
          }
          else {
            if ((Integer)info.get(46+j+1) == 3) {
              inventory.add(new InvSlot(new Body((String)info.get(46+j+2), (Integer)info.get(46+j+3), (Integer)info.get(46+j+4), (Integer)info.get(46+j+5), (Integer)info.get(46+j+6))));
              j = j + 6;
            }
            else {
              if ((Integer)info.get(46+j+1) == 4) {
                inventory.add(new InvSlot(new Gloves((String)info.get(46+j+2), (Integer)info.get(46+j+3), (Integer)info.get(46+j+4), (Integer)info.get(46+j+5), (Integer)info.get(46+j+6))));
                j = j + 6;
              }
              else {
                if ((Integer)info.get(46+j+1) == 5) {
                  inventory.add(new InvSlot(new Shoes((String)info.get(46+j+2), (Integer)info.get(46+j+3), (Integer)info.get(46+j+4), (Integer)info.get(46+j+5), (Integer)info.get(46+j+6))));
                  j = j + 6;
                }
                else {
                  if ((Integer)info.get(46+j+1) == 6) {
                    inventory.add(new InvSlot(new Pants((String)info.get(46+j+2), (Integer)info.get(46+j+3), (Integer)info.get(46+j+4), (Integer)info.get(46+j+5), (Integer)info.get(46+j+6))));
                    j = j + 6;
                  }
                  else {
                    if ((Integer)info.get(46+j+1) == 7) {
                      inventory.add(new InvSlot(new Accessory((String)info.get(46+j+2), (Integer)info.get(46+j+3), (Integer)info.get(46+j+4), (Integer)info.get(46+j+5), (Integer)info.get(46+j+6))));
                      j = j + 6;
                    }
                  }
                }
              }
            }
          }
        }
        tracker++;
      }  
      
    }
        catch(Exception e) {
          name = file;
          equip(new Body());
          equip(new Helm());
          equip(new Gloves());
          equip(new Pants());
          equip(new Shoes());
          equip(new Accessory());
          abilities.add("Lightning");
          abilities.add("Fire");
          abilities.add("Heal");
          Weapon pipe = new Weapon("Pipe", 5, 10, 0, 0, 0, 0);
          inventory.add(new InvSlot(new Body("Chest", 0, 0, 0, 0)));
          inventory.add(new InvSlot(new Pants("Leggings", 0, 0, 0, 0)));
          equip(pipe);
          cls.saveChar(this);
          PrintWriter writer = null;
          try {
            writer = appendWriter("CList.txt");
            writer.print(name+",");
          } 
          catch(Exception ee) {
          }
          writer.close();
        }
      }

      public ArrayList<InvSlot> getInventory() {
        return inventory;
      }

      public int getMoney() {
        return myMoney;
      }

      /*Thank you, PhiLho of the processing forum*/
      private PrintWriter appendWriter(String filename) {
        try {
          File file = saveFile(filename);
          OutputStream output = new FileOutputStream(file, true);
          return createWriter(output);
        } 
        catch (Exception e) {
          e.printStackTrace();
          throw new RuntimeException("Couldn't create a writer for " + filename);
        }
      }
    }

